package com.fzrskj.cloud.free.dic.utils;

import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.enums.PromotionEnum;
import com.fzrskj.cloud.free.sys.bean.SysPromotion;
import com.fzrskj.cloud.free.sys.utils.SysPromotionUtils;
import ms.core.object.CObject;
import ms.core.tool.DoubleUtil;
import ms.core.tool.StrTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DicPriceVHostUtils.
 *
 * @author zj.
 *         Created on 2018/10/17 0017.
 */
public class DicPriceVHostUtils extends CObject {
    /**
     * 获取优惠时长.
     * @param productCode
     * @param chargeId
     * @param applyTime
     * @param priceType
     * @return
     */
    public static Integer getPromotionDays(String productCode, Integer chargeId, Integer applyTime, String priceType) {
        SysPromotionUtils promotionUtils=new SysPromotionUtils();
        List<SysPromotion> promotionList=new ArrayList<>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("productCode", productCode);
        param.put("chargeId", chargeId);
        promotionList=promotionUtils.getSysPromotionList(param);
        //Boolean exists = false;
        Integer saleType = 0;
        Double saleValue = Double.valueOf(0);
        if (!promotionList.isEmpty()) {
            for (SysPromotion sysPromotion : promotionList) {
                if (applyTime.equals(sysPromotion.getApplyTime()) && priceType.equals(sysPromotion.getApplyType())) {
                    saleType = sysPromotion.getSaleType();
                    if (!StrTool.isBlankStr(sysPromotion.getSaleValue())) {
                        saleValue = Double.valueOf(sysPromotion.getSaleValue());
                    }
                }
            }
        }

        Integer days = 0;

        if (PromotionEnum.MONTH.equals(saleType)) {
            days = DoubleUtil.mul(Double.valueOf(CConsts.MONTH_DAY), saleValue).intValue();
        } else if (PromotionEnum.DAY.equals(saleType)) {
            days = saleValue.intValue();
        }

        return days;
    }
}
